#include <gio/gio.h>

int main (int argc, char *argv[]) {
    gint ret = 1;
    gchar opt_address[] = "unix:abstract=/org/gtk/GDBus/address";
    GError *error = NULL;

    GDBusConnection *connection;
    const gchar *greeting_response;
    GVariant *value;
    gchar *greeting;

    error = NULL;
    connection = g_dbus_connection_new_for_address_sync (opt_address,
                                                         G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
                                                         NULL, /* GDBusAuthObserver */
                                                         NULL, /* GCancellable */
                                                         &error);
    if (connection == NULL) {
        g_printerr ("Error connecting to D-Bus address %s: %s\n", opt_address, error->message);
        g_error_free (error);
        goto out;
    }

    g_print ("Connected.\n"
             "Negotiated capabilities: unix-fd-passing=%d\n",
             g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);

    greeting = g_strdup_printf ("Hey, it's %" G_GINT64_FORMAT " already!",
            g_get_real_time () / G_USEC_PER_SEC);
    value = g_dbus_connection_call_sync (connection,
                                         NULL, /* bus_name */
                                         "/org/gtk/GDBus/TestObject",
                                         "org.gtk.GDBus.TestPeerInterface",
                                         "HelloWorld",
                                         g_variant_new ("(s)", greeting),
                                         G_VARIANT_TYPE ("(s)"),
                                         G_DBUS_CALL_FLAGS_NONE,
                                         -1,
                                         NULL,
                                         &error);
    if (value == NULL) {
        g_printerr ("Error invoking HelloWorld(): %s\n", error->message);
        g_error_free (error);
        goto out;
    }
    g_variant_get (value, "(&s)", &greeting_response);
    g_print ("Server said: %s\n", greeting_response);
    g_variant_unref (value);

    g_object_unref (connection);

    ret = 0;

    out:
    return ret;
}